﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.InteropServices;

namespace WFFS
{
    public partial class FrmSendMSG : Form
    {
        private Form1 frmMain = null;
        private FrmMsg frmMsg = null;
        public FrmSendMSG()
        {
            InitializeComponent();
        }
        public FrmSendMSG(Form1 _frm,FrmMsg _frmmsg)
        {
            InitializeComponent();
            frmMain = _frm;
            frmMsg = _frmmsg;
        }

        //初始化人员
        private void FrmSendMSG_Load(object sender, EventArgs e)
        {
            //判断片区数量
            string sql = " select distinct Section from UserInfo  where Section is not null and Section <>''";
            //SqlDataReader reader = null;
            SqlCommand cmd = new SqlCommand(sql, frmMain.dbConnect);                        
            SqlDataAdapter datapter01 = new SqlDataAdapter(cmd);
            DataSet ds01 = new DataSet();
            datapter01.Fill(ds01);
            int isec = 1;
            for (int j = 0; j < ds01.Tables[0].Rows.Count; j++)
            {
                string section = ds01.Tables[0].Rows[j]["Section"].ToString();
                string sql1 = "select RFID,CarRFID,UserName,isLeader,ipaddr from UserInfo where Section='" + section + "' and RFID<>'0000' order by isLeader desc";
                SqlDataReader reader1 = null;
                SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
                reader1 = cmd1.ExecuteReader();
                int i = 0;
                int l = 0;
                int m = 0;
                while (reader1.Read())
                {
                    string name = reader1["UserName"].ToString();
                    string rfid = reader1["RFID"].ToString();
                    string leader = reader1["isLeader"].ToString();

                    CheckBox b = new CheckBox();
                    if (l / 3 > 0)
                    {
                        m++;
                        i = 0;
                        l = 0;
                    }
                    b.Location = new Point(5 + i * 68, 10 + m * 30);
                    b.Size = new System.Drawing.Size(60, 16);
                    b.Tag = leader + "," + reader1["ipaddr"].ToString();
                    b.Name = "ck_user";
                    b.Text = name;
                    if (isec == 1)
                    {
                        this.panelOne.Controls.Add(b);
                        pictureBox7.BackgroundImage = imageList1.Images[int.Parse(section)];
                        panel4.Visible = true;
                    }
                    if (isec == 2)
                    {
                        this.panelTwo.Controls.Add(b);
                        pictureBox8.BackgroundImage = imageList1.Images[int.Parse(section)];
                        panel3.Visible = true;
                    }
                    if (isec == 3)
                    {
                        this.panelThree.Controls.Add(b);
                        pictureBox9.BackgroundImage = imageList1.Images[int.Parse(section)];
                        panel6.Visible = true;
                    }
                    if (isec == 4)
                    {
                        this.panelFour.Controls.Add(b);
                        pictureBox10.BackgroundImage = imageList1.Images[int.Parse(section)];
                        panel13.Visible = true;
                    }
                    if (isec == 5)
                    {
                        this.panelFive.Controls.Add(b);
                        pictureBox11.BackgroundImage = imageList1.Images[int.Parse(section)];
                        this.Width = 586;
                        this.Height = 416;
                        panel16.Visible = true;
                    }
                    if (isec == 6)
                    {
                        this.panelSix.Controls.Add(b);
                        pictureBox12.BackgroundImage = imageList1.Images[int.Parse(section)];
                        this.Width = 586;
                        this.Height = 479;
                        panel5.Visible = true;
                    }
                    i++;
                    l++;
                }
                reader1.Close();
                isec++;
            }
            ds01.Clear();
            ds01 = null;
            System.GC.Collect();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Control[] controls = panel1.Controls.Find("ck_user", true);
            foreach (Control con in controls)
            {
                if (con is CheckBox)
                {
                    ((CheckBox)con).Checked = true;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button3_Click(this,e);

            Control[] controls1 = panel1.Controls.Find("ck_user", true);
            foreach (Control con in controls1)
            {
                if (con is CheckBox)
                {
                    CheckBox tmpBox = (CheckBox)con;
                    if (tmpBox.Tag.ToString().Split(',')[0] == "1")
                    {
                        tmpBox.Checked = true;
                    }
                    else
                    {
                        tmpBox.Checked = false;
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Control[] controls = panel1.Controls.Find("ck_user", true);
            foreach (Control con in controls)
            {
                if (con is CheckBox)
                {
                    ((CheckBox)con).Checked = false;
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            button3_Click(this, e);           
            richTBOrder.Text = "";
            panelPicOrder.Controls.Clear();
        }

        private void btnOrder_Click(object sender, EventArgs e)
        {
            if (richTBOrder.Text == "" && panelPicOrder.Controls.Count <=0)
                return;

            //取得当前选中的消息接收人员
            string reciveUser = "";
            Control[] controls = panel1.Controls.Find("ck_user", true);
            foreach (Control con in controls)
            {
                if (con is CheckBox)
                {
                    CheckBox tmpBox = (CheckBox)con;
                    string[] tmpIpInfo = tmpBox.Tag.ToString().Split(',');
                    if (tmpBox.Checked)
                    {
                        if (reciveUser == "")
                        {
                            if (tmpIpInfo[1] != frmMain.LocalIP)   //自己不传
                                reciveUser = tmpIpInfo[1];
                        }
                        else
                        {
                            if (tmpIpInfo[1] != frmMain.LocalIP)
                                reciveUser += "," + tmpIpInfo[1];
                        }
                    }
                }
            }

            string sendtime = DateTime.Now.ToLongTimeString().ToString();

            string msg = "";
            string picmsg = "";
            msg = reciveUser + "|" + frmMain.MyName + "(" + sendtime + ")：" + richTBOrder.Text;

            //客户端消息发送
            if (frmMain.socket == null)
            {
                if (frmMain.InfoConnectServer() != true)
                {
                    MessageBox.Show(this, "指挥中心未能连接上，请稍侯再试。。。");
                    return;
                }
            }
            if (frmMsg.msg == "")
            {
                if (richTBOrder.Text != "")
                    frmMain.socket.ASend(msg + AsySocket.EndChar);
                    //frmMsg.msg = "指挥中心(" + sendtime + ")：" + richTBOrder.Text;
            }
            else
            {
               if (richTBOrder.Text != "")
                   frmMain.socket.ASend(msg + AsySocket.EndChar);
                   //frmMsg.msg = "指挥中心(" + sendtime + ")：" + richTBOrder.Text + Environment.NewLine + frmMsg.msg;
            }

            //判断有无图标命令
            if (frmMsg.picmsg == "")
            {
                if (panelPicOrder.Controls.Count > 0)
                {
                    picmsg = reciveUser + "|" + frmMain.MyName + "(" + sendtime + ")：" + panelPicOrder.Controls[0].Name;
                    frmMain.socket.ASend(picmsg + AsySocket.EndChar);
                    //frmMsg.picmsg = "客户端(" + sendtime + ")：" + panelPicOrder.Controls[0].Name;
                }
            }
            else
            {
                if (panelPicOrder.Controls.Count > 0)
                {
                    picmsg = reciveUser + "|" + frmMain.MyName + "(" + sendtime + ")：" + panelPicOrder.Controls[0].Name;
                    frmMain.socket.ASend(picmsg + AsySocket.EndChar);
                    //frmMsg.picmsg = "客户端(" + sendtime + ")：" + panelPicOrder.Controls[0].Name;
                }
            }
            panelPicOrder.Controls.Clear();

            if (frmMsg.Visible) //若子窗体显示d
            {
                frmMsg.Hide();
                frmMsg.Show();
                frmMsg.Refresh();
            }
            else
            {

            }
            richTBOrder.Text = "";
        }
        private void cbbOrder_SelectedIndexChanged(object sender, EventArgs e)
        {
            richTBOrder.Text = cbbOrder.Text;
        }

        private void genPic(object sender, EventArgs e,string no)
        {
            panelPicOrder.Controls.Clear();
            string cn = "pictureBox" + no;
            PictureBox pb = new PictureBox();
            
            PictureBox pb1 = new PictureBox();
            foreach (Control c in groupBoxPic.Controls)
            {
                if (c.Name == cn)
                    pb1 = (PictureBox)c;
            }
            pb.BackgroundImage = pb1.BackgroundImage;

            pb.Size = new System.Drawing.Size(40, 40);
            pb.Name = cn;
            this.panelPicOrder.Controls.Add(pb);
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            genPic(sender, e, "1");
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            genPic(sender, e, "2");
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            genPic(sender, e, "3");
        }
       
        private void pictureBox4_Click(object sender, EventArgs e)
        {
            genPic(sender, e, "4");
        }
        private void pictureBox5_Click(object sender, EventArgs e)
        {
            genPic(sender, e, "5");
        }
        private void pictureBox6_Click(object sender, EventArgs e)
        {
            genPic(sender, e, "6");
        }

        private void FrmSendMSG_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmSendMSG", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmSendMSG", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}
